﻿using System;
using System.IO;
using System.Linq;

namespace IcqSharp
{
    public static class StreamExtensions
    {
        public static UInt16 Available(this MemoryStream s)
        {
            return (UInt16)(s.Length - s.Position);
        }

        public static new Byte ReadByte(this Stream s)
        {
            return (Byte)s.ReadByte();
        }
        public static Byte[] ReadBytes(this Stream s, UInt16 len)
        {
            var buf = new Byte[len];
            s.Read(buf, 0, len);
            return buf;
        }
        public static UInt16 ReadUInt16(this Stream s)
        {
            var buf = new Byte[2]; s.Read(buf, 0, 2);
            buf = buf.Reverse().ToArray();
            return BitConverter.ToUInt16(buf, 0);
        }
        public static UInt32 ReadUInt32(this Stream s)
        {
            var buf = new Byte[4]; s.Read(buf, 0, 4);
            buf = buf.Reverse().ToArray();
            return BitConverter.ToUInt32(buf, 0);
        }
        public static String ReadAsciiString(this Stream s, UInt16 len)
        {
            var tmp = new Byte[len];
            s.Read(tmp, 0, len);

            return tmp.ToAsciiString();
        }
        public static String ReadUnicodeString(this Stream s, UInt16 len)
        {
            var tmp = new Byte[len * 2];
            s.Read(tmp, 0, len * 2);

            return tmp.ToUnicodeString();
        } 

        public static void Skip(this MemoryStream s, Int16 offset)
        {
            s.Seek(offset, SeekOrigin.Current);
        }

        public static void Write(this Stream s, Byte[] value)
        {
            s.Write(value, 0, value.Length);
        }
        public static void Write(this Stream s, Byte value)
        {
            s.WriteByte(value);
        }
        public static void Write(this Stream s, UInt16 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.Reverse().ToArray();
            s.Write(buf, 0, 2);
        }
        public static void Write(this Stream s, UInt32 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.Reverse().ToArray();
            s.Write(buf, 0, 4);
        }
        public static void Write(this Stream s, Int32 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.Reverse().ToArray();
            s.Write(buf, 0, 4);
        }

        public static void WriteLE(this Stream s, UInt16 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.ToArray();
            s.Write(buf, 0, 2);
        }
        public static void WriteLE(this Stream s, UInt32 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.ToArray();
            s.Write(buf, 0, 4);
        }
        public static void WriteLE(this Stream s, Int32 value)
        {
            var buf = BitConverter.GetBytes(value);
            buf = buf.ToArray();
            s.Write(buf, 0, 4);
        }


        public static void WriteTLV(this Stream s, UInt16 type, UInt16 value)
        {
            s.Write(type);
            s.Write((UInt16)2);
            s.Write(value);
        }
        public static void WriteTLV(this Stream s, UInt16 type, UInt32 value)
        {
            s.Write(type);
            s.Write((UInt16)4);
            s.Write(value);
        }
        public static void WriteTLV(this Stream s, UInt16 type, Byte[] value)
        {
            s.Write(type);
            s.Write((UInt16)value.Length);
            s.Write(value);
        }
    }


    
}
